package com.guigu.erp.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.guigu.erp.dto.StoragePayDetailsDto;
import com.guigu.erp.pojo.StorageCell;
import com.guigu.erp.pojo.StoragePay;
import com.guigu.erp.pojo.StoragePayDetails;
import com.guigu.erp.service.StorageCellService;
import com.guigu.erp.service.StoragePayDetailsService;
import com.guigu.erp.service.StoragePayService;
import com.guigu.erp.utils.PageUtils;
import com.guigu.erp.vo.StoragePayVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@RequestMapping("/storage/pay")
@Controller
public class StoragePayController {
    @Autowired
    private StoragePayService storagePayService;

    @Autowired
    private StoragePayDetailsService storagePayDetailsService;

    @Autowired
    private StorageCellService storageCellService;

    @RequestMapping("selectStoragePay")
    @ResponseBody
    public PageUtils<StoragePay> selectStoragePay(StoragePayVo vo,Integer pageNo,Integer pageSize){
        PageUtils<StoragePay> pageUtils = storagePayService.queryPay(vo, pageNo, pageSize);
        return pageUtils;
    }

    //根据id查询出库设计单
    @RequestMapping("getPayId")
    @ResponseBody
    public StoragePay getPayId(Integer payId){
        StoragePay storagePay = storagePayService.getById(payId);
        return storagePay;
    }

    //根据id查询出库明细
    @RequestMapping("getIdQueryPayDetails")
    @ResponseBody
    public List<StoragePayDetails> getIdQueryPayDetails(Integer payId){
        QueryWrapper<StoragePayDetails> wrapper = new QueryWrapper<>();
        wrapper.eq("PARENT_ID",payId);
        List<StoragePayDetails> list = storagePayDetailsService.list(wrapper);
        return list;
    }

    //判断是否制定安全配置单 库存是否够
    @RequestMapping("judgmentMakeConfigurationOrder")
    @ResponseBody
    public boolean judgmentMakeConfigurationOrder(Integer detailId){
        StoragePayDetails payDetails = storagePayDetailsService.getById(detailId);
        QueryWrapper<StorageCell> wrapper = new QueryWrapper<>();
        wrapper.eq("PRODUCT_ID",payDetails.getProductId());
        
        StorageCell cell = storageCellService.getOne(wrapper);
        if (cell==null){
            return false;
        }
        if(payDetails.getAmount().compareTo(cell.getAmount()) == 1){
            return false;
        }
        return cell.getCheckTag().equals("S001-1");
    }
    //根据明细id查询明细数据
    @RequestMapping("getDetailId")
    @ResponseBody
    public StoragePayDetails getDetailId(Integer detailId){
        StoragePayDetails payDetails = storagePayDetailsService.getById(detailId);
        return payDetails;
    }

    //点击修改出库/出库明细/库存单元
    @RequestMapping("modifyPay")
    @ResponseBody
    public boolean modifyPay(StoragePayDetailsDto dto){
        boolean flag = storagePayDetailsService.ModifyTheOutboundDetails(dto);
        return flag;
    }

    //判断是否调度完成
    @RequestMapping("judgmentComplete")
    @ResponseBody
    public boolean judgmentComplete(String pid){
        QueryWrapper<StoragePay> wrapper = new QueryWrapper<>();
        wrapper.eq("PAY_ID",pid);
        StoragePay pay = storagePayService.getOne(wrapper);
        return pay.getStoreTag().equals("K002-2");
    }
}
